<?php
namespace App\Http\Controllers\Business\Admin\User;

use App\Models\AdminUser;
use App\Models\AppStart;
use App\Models\Banner;
use App\Models\DoubleCoupon;
use App\Models\Order;
use App\Models\Recharge;
use App\Models\RedEnvelope;
use App\Models\RedEnvelopeSetLog;
use App\Models\Comment;
use App\Models\User;
use App\Models\City;
use App\Models\Oss;
use App\Models\Shop;
use App\Models\Refund;
use Auth;
use Exception;

class UserMamagement{

    //用户列表
    public static function userList($startTime = '',$lastTime = '',$status = '%',$search = '',$skip=0, $take=200) {
        if(!$startTime || !$lastTime) {
            $startTime = '2016-01-01 00:00:00';
            $lastTime = date('Y-m-d H:i:s',time());
        }
        //南昌总部
        if ($search) {
            $GLOBALS['search'] = $search;
            $userList = User::where('status', 'like', $status)
                ->where(function ($query) {
                    $query->orWhere('mobile', $GLOBALS['search'])
                        ->orWhere('nickname', 'like', '%' . $GLOBALS['search'] . '%');
                })
                ->select('id as userid', 'mobile', 'nickname', 'balance', 'point', 'registerarea', 'created_at', 'issalesman', 'isdouble', 'status', 'shopid','salesmanid')
                ->orderBy('created_at', 'desc')
                ->skip($skip)->take($take)->get();
        } else {
            $userList = User::where('created_at', '>', $startTime)
                ->where('created_at', '<', $lastTime)
                ->where('status', 'like', $status)
                ->select('id as userid', 'mobile', 'nickname', 'balance', 'point', 'registerarea', 'created_at', 'issalesman', 'isdouble', 'status', 'shopid','salesmanid')
                ->orderBy('created_at', 'desc')
                ->skip($skip)->take($take)->get();
        }
        foreach($userList as $k=>$v) {
            //$userlist[$k]['mobile'] = str_replace(substr($userlist[$k]['mobile'], 3, 4), '****', $userlist[$k]['mobile']);
            $userList[$k]['ordercount'] = Order::where('userid', $userList[$k]['userid'])->where('refundstatus',0)->count();
            $userList[$k]['actualamount'] = Order::where('userid', $userList[$k]['userid'])->where('refundstatus',0)->sum('actualamount');
        }

        return ['status' => 'success', 'data' => $userList];
    }
    //通过起始时间获取用户数量
    public static function getUserCountByFromDateAndToDate($cityId, $starttime, $lasttime) {
        if(!$starttime || !$lasttime) {
            $starttime = '2016-01-01 00:00:00';
            $lasttime = date('Y-m-d H:i:s',time());
        }
        if($cityId == 1) {
            //南昌总部
            $data = User::where('created_at', '>=', $starttime)->where('created_at', '<=', $lasttime)->count();
        } else {
            //分部
            $cityName = City::where('id', $cityId)->pluck('name')->first();
            $data = User::where('registerarea', $cityName)->where('created_at', '>=', $starttime)->where('created_at', '<=', $lasttime)->count();
        }
        return $data;
    }
    //地区用户列表
    public static function regionUserList($cityId, $startTime = '',$lastTime = '',$search = '',$skip=0, $take=200) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime || !$lastTime) {
            $startTime = '2016-01-01 00:00:00';
            $lastTime = date('Y-m-d H:i:s',time());
        }
        $cityName = City::where('id', $cityId)->pluck('name')->first();
        if ($search) {
            $GLOBALS['search'] = $search;
            $userList = User::where('registerarea', $cityName)
                ->where(function ($query) {
                    $query->orWhere('mobile', $GLOBALS['search'])
                        ->orWhere('nickname', 'like', '%' . $GLOBALS['search'] . '%');
                })
                ->where('registerarea', $cityName)
                ->select('id as userid', 'mobile', 'nickname', 'registerarea', 'created_at', 'status', 'shopid','salesmanid')
                ->orderBy('created_at', 'desc')
                ->skip($skip)->take($take)->get();
        } else {
            $userList = User::where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $lastTime)
                ->where('registerarea', $cityName)
                ->select('id as userid', 'mobile', 'nickname', 'registerarea', 'created_at', 'status', 'shopid','salesmanid')
                ->orderBy('created_at', 'desc')
                ->skip($skip)->take($take)->get();
        }
        foreach($userList as $k=>$v) {
            $userList[$k]['mobile'] = str_replace(substr($userList[$k]['mobile'], 3, 4), '****', $userList[$k]['mobile']);
            $userList[$k]['ordercount'] = Order::where('userid', $userList[$k]['userid'])->where('refundstatus',0)->count();
            $userList[$k]['orderamount'] = Order::where('userid', $userList[$k]['userid'])->where('refundstatus',0)->sum('actualamount');
        }
        return ['status' => 'success', 'data' => $userList];
    }
    //通过起始时间获取地区用户数量
    public static function getRegionUserCountByFromDateAndToDate($cityId, $startTime, $lastTime, $search='') {
        if(!$search) {
            if (!$startTime || !$lastTime) {
                $startTime = '2016-01-01 00:00:00';
                $lastTime = date('Y-m-d H:i:s');
            }
            $cityName = City::where('id', $cityId)->pluck('name')->first();
            $data = User::where('registerarea', $cityName)->where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)->count();
        } else {
            $GLOBALS['search'] = $search;
            $cityName = City::where('id', $cityId)->pluck('name')->first();
            $data = User::where('registerarea', $cityName)->where(function($query){
                $query->orWhere('mobile', $GLOBALS['search'])
                    ->orWhere('nickname', 'like', '%' . $GLOBALS['search'] . '%');
            })->count();
        }
        return $data;
    }
    //通过用户id获取用户详情
    public static function getUser($userid) {
        if(!$userid) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        $user = User::where('id', $userid)
            ->select('id as userid','mobile','nickname','balance','point','created_at','issalesman','isdouble','status','sex','birthday', 'city', 'registerarea', 'shopid', 'logintime','salesmanid')
            ->first();
        $user['account'] = array(
            'rechargetotal'=>Recharge::where('userid',$userid)->sum('money'),
            'doubletotal'=>Recharge::where('userid',$userid)->where('doublestatus','1')->sum('doublemoney'),
            'waitdoubletotal'=>Recharge::where('userid',$userid)->where('doublestatus','0')->sum('doublemoney'),
            'ordertotal'=>Order::where('userid',$userid)->where('refundstatus',0)->sum('actualamount'),
            'redenvelopebalance' => RedEnvelope::where('userid', $userid)->where('status', 4)->sum('amount'),
        );
        $user['doublecoupon'] = array(
            'doublecouponsum'=>DoubleCoupon::where('userid',$userid)->get()->count(),
            'usedoublecouponsum'=>DoubleCoupon::where('userid',$userid)->where('status','1')->get()->count(),
            'expireddoublecouponsum'=>DoubleCoupon::where('userid',$userid)->where('endtime','<',date('Y-m-d H:i:s'))->get()->count(),
            'doublecoupon'=>DoubleCoupon::where('userid',$userid)->where('status','0')->get()->count()
        );
        $user['redenvelope'] = array(
            'redenvelopesum'=>RedEnvelope::where('userid',$userid)->get()->count(),
            'useredenvelopesum'=>RedEnvelope::where('userid',$userid)->where('status','1')->get()->count(),
            'useredenvelopetotal'=>RedEnvelope::where('userid',$userid)->where('status','1')->sum('amount'),
            'expireredenvelopesum'=>RedEnvelope::where('userid',$userid)->where('endtime','<',date('Y-m-d H:i:s'))->get()->count(),
            'expireredenvelopetotal'=>RedEnvelope::where('userid',$userid)->where('endtime','<',date('Y-m-d H:i:s'))->sum('amount'),
            'redenvelope'=>RedEnvelope::where('userid',$userid)->where('status','0')->get()->count(),
            'redenvelopetotal'=>RedEnvelope::where('userid',$userid)->where('status','0')->sum('amount'),
        );
        $user['saletype'] = 0;
        if(isset($user['shopid']) && ($user['shopid'] != 0)) {
            $shopInfo = Shop::where('shops.id', $user['shopid'])->join('city','city.id','=','shops.cityid')->select('code','city.name')->first();
            $user['shopcode'] = $shopInfo['code'];
            $user['cityname'] = $shopInfo['name'];
            $user['saletype'] = 1;
        } else {
            $user['shopcode'] = '';
            $user['cityname'] = '';
        }
        if(isset($user['salesmanid']) && ($user['salesmanid'] !=0)){
            $salemanInfo = User::where('id',$user['salesmanid'])->select('nickname','mobile')->first();
            $user['salemanname'] = $salemanInfo['nickname'];
            $user['salemobile'] = $salemanInfo['mobile'];
            $user['saletype'] = 2;
        }else{
            $user['salemanname'] = '';
            $user['salemobile'] = '';
        }
        return ['status' => 'success', 'data' => $user];
    }
    //通过类型（充值，消费，评论， 红包）获取用户信息
    public static function getUserInfoByType($userId, $type, $skip, $take) {
        if(!$userId || !$type) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if($type == 'recharge') {
            //充值
            $data = Recharge::where('userid', $userId)->orderBy('created_at', 'desc')
                ->select('rechargenumber', 'path', 'money', 'fee', 'actualmoney', 'status', 'created_at')
                ->skip($skip)->take($take)->get();
        } elseif($type == 'order') {
            //订单
            $data = Order::where('orders.userid', $userId)->orderBy('orders.created_at', 'desc')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->join('city', 'shops.cityid', '=', 'city.id')
                ->select('orders.ordernumber','shops.code', 'city.name as cityname', 'shops.name as shopname','orders.path', 'orders.orderamount',
                    'orders.redenvelopeamount', 'orders.actualamount', 'orders.bonus', 'orders.refundstatus','orders.created_at')
                ->skip($skip)->take($take)->get();
        } elseif($type == 'comment') {
            //评论
            $data = Comment::where('comments.userid', $userId)->orderBy('comments.created_at', 'desc')->where('comments.isdel', 0)
                ->join('shops', 'comments.shopid', '=', 'shops.id')
                ->select('comments.ordernumber', 'shops.code', 'shops.name as shopname', 'comments.score', 'comments.content', 'comments.picture','comments.created_at')
                ->skip($skip)->take($take)->get();
        } elseif($type == 'redenvelope') {
            //红包
            $data = RedEnvelope::where('userid', $userId)->orderBy('created_at', 'desc')
                ->select('amount', 'shopid', 'minimumamount', 'starttime', 'endtime', 'cityid', 'status', 'created_at')
                ->skip($skip)->take($take)->get();
            foreach($data as $k => $v) {
                $data[$k]['cityname'] = City::where('id', $data[$k]['cityid'])->pluck('name')->first();
            }
        } elseif($type == 'refund') {
            $data = Refund::where('userid', $userId)->orderBy('created_at', 'desc')
                ->select('id','created_at', 'money', 'fee', 'actualmoney', 'startbalance', 'lastbalance', 'note')
                ->skip($skip)->take($take)->get();
        } else {
            return ['status'=>'failed', 'message'=>'类型不对'];
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //更新用户信息
    public static function updateUser($userid,$issalesman='',$isdouble='',$status='') {
        if(!$userid) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        $user = User::where('id', $userid)->first();
        $user->issalesman = $issalesman;
        $user->isdouble = $isdouble;
        $user->status = $status;
        $user->adminid = Auth::guard('adminweb')->id();
        $user->save();
        return ['status' => 'success', 'message'=>'修改成功'];
    }
    //更改用户翻倍状态
    public static function updateUserIsDouble($userId, $isDouble) {
        if(!$userId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        User::where('id', $userId)->update(['isdouble'=>$isDouble]);
        return ['status'=>'success', 'message'=>'修改成功'];
    }
    //更新用户状态
    public static function updateUserStatus($userid, $status='0') {
        if(!$userid) {
            return ['status'=>'failed', 'message'=>'参数不对！'];
        }
        User::where('id', $userid)->update(['status'=>$status]);
        return ['status'=>'success', 'message'=>'修改成功'];
    }
    //获取代理商红包设置
    public static function getAgentRedEnvelope($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $data = City::where('id', $cityId)->select('id', 'name', 'agentratio', 'hqratio', 'ratio', 'validdays')->first();
        return ['status'=>'success', 'data'=>$data];
    }
    //编辑代理商红包设置
    public static function updateAgentRedEnvelopeAgentRatio($cityId, $agentRatio, $validDays) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $city = City::where('id', $cityId)->first();
        if(!$city) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $hqRatio = $city->hqratio;
        $oldAgentRatio = $city->agentratio;
        $oldValidDays = $city->validdays;
        $ratio = $agentRatio + $hqRatio;
        $city->agentratio = $agentRatio;
        $city->ratio = $ratio;
        $city->validdays = $validDays;
        $city->save();
        //写日志
        $content = '代理商红包比率由' . $oldAgentRatio . "%变成了" . $agentRatio ."%;" . "有效期限由" . $oldValidDays ."天变成了" .$validDays."天";
        self::writeRedEnvelopeSetLog($cityId, $agentRatio, $hqRatio, $ratio, $validDays, $content);
        return ['status'=>'success', 'message'=>'修改成功'];
    }
    //修改红包比例写日志
    public static function writeRedEnvelopeSetLog($cityId, $agentRatio, $hqRatio, $ratio, $validDays, $content='') {
        if(!$cityId||($agentRatio+$hqRatio != $ratio)) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $redEnvelopeSetLog = RedEnvelopeSetLog::create([
            'cityid' => $cityId,
            'agentratio' => $agentRatio,
            'hqratio' => $hqRatio,
            'ratio' => $ratio,
            'validdays' => $validDays,
            'content' => $content,
            'adminid' => Auth::guard('adminweb')->id()
        ]);
        $redEnvelopeSetLog->save();
        return ['status'=>'success', 'message'=>'新增成功'];
    }
    //红包设置记录列表
    public static function redEnvelopeLogList($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $lists = RedEnvelopeSetLog::where('red_envelope_set_logs.cityid', $cityId)
            ->join('admin_users', 'red_envelope_set_logs.adminid', '=', 'admin_users.id')
            ->join('city', 'red_envelope_set_logs.cityid', '=', 'city.id')
            ->select('red_envelope_set_logs.*', 'admin_users.realname as adminname', 'city.name as cityname')
            ->orderBy('red_envelope_set_logs.created_at', 'desc')->get();
        return ['status'=>'success', 'data'=>$lists];
    }
    //首页banner列表
    public static function bannerList($cityid) {
        if(AdminUser::find(Auth::guard('adminweb')->id())->pluck('isalldata')) {
            $bannerlist = Banner::orWhere('banners.cityid',$cityid)->orWhere('banners.cityid','')
                ->join('admin_users','banners.adminid','=','admin_users.id')
                ->select('banners.id as bannerid','banners.name','banners.url','banners.isglobal','banners.type','banners.sort','banners.status','banners.starttime','banners.created_at','admin_users.realname')
                ->orderBy('banners.status', 'desc')
                ->orderBy('banners.isglobal', 'desc')
                ->get();
        } else {
            $bannerlist = Banner::Where('cityid',$cityid)
                ->select('banners.id as bannerid','banners.name','banners.url','banners.isglobal','banners.type','banners.sort','banners.status','banners.starttime','banners.created_at','admin_users.realname')
                ->join('admin_users','banners.adminid','=','admin_users.id')
                ->orderBy('banners.status', 'desc')
                ->orderBy('banners.isglobal', 'desc')
                ->get();
        }
        return ['status' => 'success','data'=>$bannerlist];
    }
    //新增banner
    public static function addBanner($name,$url,$isglobal,$cityid,$type,$sort, $islogin, $starttime, $content='') {
        if($isglobal) {
            if(Banner::where('isglobal','1')->where('status', 1)->get()->count()>=2) {
                return ['status' => 'failed', 'message' => '全国banner已达到上限！'];
            }
            $cityid = '';
        }
        if($cityid) {
            if(Banner::where('cityid',$cityid)->where('status', 1)->get()->count()>=4) {
                return ['status' => 'failed', 'message' => '该城市banner已达到上限！'];
            }
        }
        if(!$name||!$url||!$type||!$sort || (!$isglobal&&!$cityid)) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        $adminUser = Auth::guard('adminweb')->user();
        $roleId = $adminUser->roleid;
        if(($roleId != 1) && ($roleId != 2) && ($roleId != 5) && ($roleId != 7) && ($isglobal == 1)) {
            return ['status'=>'failed', 'message'=>'你没有操作全国banner的权限'];
        }
        if(date('Y-m-d', strtotime($starttime)) == date('Y-m-d')) {
            $status = 1;
        } else {
            $status = 0;
        }
        $banner = new Banner();
        $banner->name = $name;
        $banner->url = $url;
        $banner->isglobal = $isglobal;
        $banner->cityid = $cityid;
        $banner->type = $type;
        $banner->sort = $sort;
        $banner->content = $content;
        $banner->adminid = Auth::guard('adminweb')->id();
        $banner->islogin = $islogin;
        $banner->starttime = date('Y-m-d', strtotime($starttime));
        $banner->status = $status;
        $banner->save();
        return ['status' => 'success', 'message'=>'新增成功'];
    }
    //通过bannerid 获取banner
    public static function getBanner($bannerid) {
        if(!$bannerid){
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        $bannerlist = Banner::where('id',$bannerid)->select('id as bannerid','name','url','isglobal','type','sort','content','islogin','status','starttime','lasttime','created_at')->first();
        return ['status' => 'success','data'=>$bannerlist];
    }
    //更新banner
    public static function updateBanner($bannerid,$name,$url,$isglobal,$cityid,$type,$sort,$islogin, $starttime, $content=''){
        if(!$bannerid) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        if(!$name||!$url||!$type||!$sort || (!$isglobal&&!$cityid)) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        $banner = Banner::where('id',$bannerid)->first();
        if($banner) {
            if($isglobal && ($banner->isglobal != 1)) {
                if(Banner::where('isglobal','1')->get()->count()>=2) {
                    return ['status' => 'failed', 'message' => '全国banner已达到上限！'];
                }
                $cityid = '';
            }
            if($cityid && ($banner->cityid != 1)) {
                if(Banner::where('cityid',$cityid)->get()->count()>=4) {
                    return ['status' => 'failed', 'message' => '该城市banner已达到上限！'];
                }
            }
            $banner->name = $name;
            $banner->url = $url;
            $banner->isglobal = $isglobal;
            $banner->cityid = $cityid;
            $banner->type = $type;
            $banner->sort = $sort;
            $banner->content = $content;
            $banner->islogin = $islogin;
            $banner->starttime = date('Y-m-d', strtotime($starttime));
            $banner->adminid = Auth::guard('adminweb')->id();
            $banner->save();
            return ['status' => 'success', 'message'=>'编辑成功'];
        } else {
            return ['status' => 'failed', 'message' => '编辑失败'];
        }
    }
    //更新banner的状态
    public static function updateBannerStatus($bannerId, $status) {
        if(!$bannerId || !$status) {
            return ['status'=>'failed', 'message'=>'参数不对'];
        }
        $result = Banner::where('id', $bannerId)->update(['status'=>$status]);
        if($result) {
            return ['status' => 'success', 'message' => '修改成功'];
        } else {
            return ['status'=>'failed', 'message'=>'修改失败'];
        }
    }
    //删除banner
    public static function delBanner($bannerid) {
        if(!$bannerid) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        $adminUser = Auth::guard('adminweb')->user();
        $roleId = $adminUser->roleid;
        $isGlobal = Banner::where('id', $bannerid)->pluck('isglobal')->first();
        if(($roleId != 1) && ($roleId != 2) && ($roleId != 5) && ($roleId != 7) && ($isGlobal == 1)) {
            return ['status'=>'failed', 'message'=>'你没有操作全国banner的权限'];
        }
        Banner::find($bannerid)->delete();
        return ['status' => 'success', 'message'=>'删除成功'];
    }
    //app启动页列表
    public static function appStartList($cityid) {
        if(AdminUser::find(Auth::guard('adminweb')->id())->pluck('isalldata')) {
            $appStartList = AppStart::where('app_start.isglobal','1')
                ->where('app_start.status', '!=', 2)
                ->where('app_start.lasttime', '>=', date('Y-m-d'))
                ->join('admin_users','app_start.adminid','=','admin_users.id')
                ->select('app_start.id as appstartid','app_start.name','app_start.picurl','app_start.isglobal','app_start.type','app_start.status','app_start.starttime','app_start.lasttime','app_start.created_at','admin_users.realname')
                ->orderBy('app_start.starttime', 'asc')
                ->get();
        } else {
            $appStartList = AppStart::Where('cityid',$cityid)
                ->where('app_start.status', '!=', 2)
                ->where('app_start.lasttime', '>=', date('Y-m-d'))
                ->join('admin_users','app_start.adminid','=','admin_users.id')
                ->select('app_start.id as appstartid','app_start.name','app_start.picurl','app_start.isglobal','app_start.type','app_start.status','app_start.starttime','app_start.lasttime','app_start.created_at','admin_users.realname')
                ->orderBy('app_start.starttime', 'asc')
                ->get();
        }
        return ['status' => 'success','data'=>$appStartList];
    }
    //新增app启动页
    public static function addAppStart($name,$url,$isglobal,$cityid,$type, $starttime, $lasttime, $content='') {
        if(!$name||!$url||!$type || (!$isglobal&&!$cityid) || !$starttime || !$lasttime) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        if($isglobal==1) {
            $cityid = '';
        }
        if(strtotime($starttime) > strtotime($lasttime)) {
            return ['status' => 'failed', 'message' => '开始时间不能大于结束时间'];
        }
        //判断时间有没有交集
        $lists = AppStart::where('cityid', $cityid)->where('status', '!=', 2)->select('starttime', 'lasttime')->get();
        foreach($lists as $list) {
            $checkStartTime = strtotime($list['starttime']);
            $checkLastTime = strtotime($list['lasttime']);
            //TODO 时间判断还有点问题
            if(($checkStartTime <= strtotime($starttime)) && ($checkLastTime >= strtotime($starttime))) {
                return ['status'=>'failed', 'message'=>'该时间段已有启动页了'];
            }
            if(($checkStartTime <= strtotime($lasttime)) && ($checkLastTime >= strtotime($lasttime))) {
                return ['status'=>'failed', 'message'=>'该时间段已有启动页了'];
            }
        }
        $appStart = new AppStart();
        $appStart->name = $name;
        $appStart->picurl = $url;
        $appStart->isglobal = $isglobal;
        $appStart->cityid = $cityid;
        $appStart->type = $type;
        $appStart->content = $content;
        $appStart->adminid = Auth::guard('adminweb')->id();
        $appStart->starttime = date('Y-m-d', strtotime($starttime));
        $appStart->lasttime = date('Y-m-d', strtotime($lasttime));
        $appStart->status = 0;
        $appStart->save();
        return ['status' => 'success', 'message'=>'新增成功'];
    }
    //通过id获取app启动页
    public static function getAppStart($appStartId) {
        if(!$appStartId){
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        $appStartList = AppStart::where('id',$appStartId)
            ->select('id as appstartid','name','picurl','isglobal','type','content','status','starttime','lasttime','created_at')
            ->first();
        return ['status' => 'success','data'=>$appStartList];
    }
    //更改app启动页
    public static function updateAppStart($appStartId,$name,$url,$isGlobal,$cityId,$type, $startTime, $lastTime, $content=''){
        if(!$appStartId) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        if(!$name||!$url||!$type || (!$isGlobal&&!$cityId)) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        if(strtotime($startTime) > strtotime($lastTime)) {
            return ['status' => 'failed', 'message' => '开始时间不能大于结束时间'];
        }
        //判断时间有没有交集
        $GLOBALS['cityid'] = $cityId;
        $lists = AppStart::where('id', '!=', $appStartId)->where('status', '!=', 2)
            ->where(function($query) {
                $query->orwhere('isglobal', 1)
                    ->orwhere('cityid', $GLOBALS['cityid']);
            })->select('starttime', 'lasttime')->get();
        foreach($lists as $list) {
            $checkStartTime = strtotime($list['starttime']);
            $checkLastTime = strtotime($list['lasttime']);
            //TODO 时间判断还有点问题
            if(($checkStartTime <= strtotime($startTime)) && ($checkLastTime >= strtotime($startTime))) {
                return ['status'=>'failed', 'message'=>'该时间段已有启动页了'];
            }
            if(($checkStartTime <= strtotime($lastTime)) && ($checkLastTime >= strtotime($lastTime))) {
                return ['status'=>'failed', 'message'=>'该时间段已有启动页了'];
            }
        }
        $appStart = AppStart::where('id',$appStartId)->first();
        if($appStart) {
            if($isGlobal && ($appStart->isglobal != 1)) {
                $cityId = '';
            }
            $appStart->name = $name;
            $appStart->picurl = $url;
            $appStart->isglobal = $isGlobal;
            $appStart->cityid = $cityId;
            $appStart->type = $type;
            $appStart->content = $content;
            $appStart->starttime = date('Y-m-d', strtotime($startTime));
            $appStart->lasttime = date('Y-m-d', strtotime($lastTime));
            $appStart->adminid = Auth::guard('adminweb')->id();
            $appStart->save();
            return ['status' => 'success', 'message'=>'编辑成功'];
        } else {
            return ['status' => 'failed', 'message' => '编辑失败'];
        }
    }
    //更改app启动页
    public static function updateAppStartStatus($appStartId, $status) {
        if(!$appStartId || !$status) {
            return ['status'=>'failed', 'message'=>'参数不对'];
        }
        $result = AppStart::where('id', $appStartId)->update(['status'=>$status]);
        if($result) {
            return ['status' => 'success', 'message' => '修改成功'];
        } else {
            return ['status'=>'failed', 'message'=>'修改失败'];
        }
    }
    //删除app启动页
    public static function delAppStart($appStartId) {
        if(!$appStartId) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        AppStart::find($appStartId)->delete();
        return ['status' => 'success', 'message'=>'删除成功'];
    }
    //迁移图片
    public static function migrateBannerPhoto($from, $to) {
        if(!$from || !$to) {
            return false;
        }
        try {
            $result = Oss::cutFile($from, $to);
            if($result) {
                return true;
            } else {
                return false;
            }
        }catch (Exception $e) {
            return false;
        }
    }
}